<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var addTwoNumbers = function (l1, l2) {
                let carry = 0
                let sum, value
                let head = new ListNode(0)
                let p = head
                while (l1 && l2) {
                    sum = carry + l1.val + l2.val
                    value = sum % 10
                    carry = parseInt(sum / 10)
                    head.next = new ListNode(value)
                    head = head.next
                    l1 = l1.next
                    l2 = l2.next
                }
                while (l1) {
                    sum = carry + l1.val
                    value = sum % 10
                    carry = parseInt(sum / 10)
                    head.next = new ListNode(value)
                    head = head.next
                    l1 = l1.next
                }
                while (l2) {
                    sum = carry + l2.val
                    value = sum % 10
                    carry = parseInt(sum / 10)
                    head.next = new ListNode(value)
                    head = head.next
                    l2 = l2.next
                }
                if (carry > 0) {
                    head.next = new ListNode(carry)
                    head = head.next
                }
                return p.next
            }
        </script>
    </body>
</html>
